<?php

namespace app\common\model;

use app\common\controller\Helper;
use app\common\enums\StatusEnum;
use think\Model;

class LangClass extends Model
{
    use Helper;

    protected $table = 'lang_class';

    // 初级班
    const CLASS_FIRST = 1;
    // 中级班
    const CLASS_SECOND = 2;
    // 高级班
    const CLASS_THIRD = 3;

    public static function attributes($attribute, $key = null)
    {
        $list = [
            'type' => [
                self::CLASS_FIRST => '初级班',
                self::CLASS_SECOND => '中级班',
                self::CLASS_THIRD => '高级班',
            ],
            'course' => [
                // 初级班
                self::CLASS_FIRST => [
                    [
                        'type' => LangClassFile::COURSE_VOICE,
                        'name' => '语音课',
                    ],
                    [
                        'type' => LangClassFile::COURSE_BASE,
                        'name' => '基础课',
                    ],
                    [
                        'type' => LangClassFile::COURSE_SENIOR,
                        'name' => '高级课',
                    ],
                ],
                // 中级班
                self::CLASS_SECOND => [
                    [
                        'type' => LangClassFile::COURSE_BASE,
                        'name' => '基础课',
                    ],
                    [
                        'type' => LangClassFile::COURSE_SENIOR,
                        'name' => '高级课',
                    ],
                ],
                // 高级班
                self::CLASS_THIRD => [
                    [
                        'type' => LangClassFile::COURSE_SENIOR,
                        'name' => '高级课',
                    ],
                ]
            ],
            'course_admin' => [
                // 初级班
                self::CLASS_FIRST => [
                    'className' => '初级班',
                    'course' => [
                        [
                            'type' => LangClassFile::COURSE_VOICE,
                            'name' => '语音课',
                        ],
                        [
                            'type' => LangClassFile::COURSE_BASE,
                            'name' => '基础课',
                        ],
                        [
                            'type' => LangClassFile::COURSE_SENIOR,
                            'name' => '高级课',
                        ],
                    ]
                ],
                // 中级班
                self::CLASS_SECOND => [
                    'className' => '中级班',
                    'course' => [
                        [
                            'type' => LangClassFile::COURSE_BASE,
                            'name' => '基础课',
                        ],
                        [
                            'type' => LangClassFile::COURSE_SENIOR,
                            'name' => '高级课',
                        ],
                    ]
                ],
                // 高级班
                self::CLASS_THIRD => [
                    'className' => '高级班',
                    'course' => [
                        [
                            'type' => LangClassFile::COURSE_SENIOR,
                            'name' => '高级课',
                        ],
                    ]
                ]
            ],
        ];
        return self::attributeReturn($list, $attribute, $key);
    }

    /**
     * 通过班级id获取班级信息和语种信息
     * @param $classIds
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getClasses($classIds)
    {
        $model = static::field('c.id, c.money, c.type, l.id AS langId, l.name AS langName')
            ->alias('c')
            ->join('lang l', 'c.lang_id = l.id')
            ->where('c.id', 'in', $classIds)
            ->where(['c.status' => StatusEnum::ENABLED, 'l.status' => StatusEnum::ENABLED])
            ->select()
            ->toArray();
        $result = [];
        foreach ($model as $key => $value) {
            $value['typeId'] = $value['type'];
            $value['type'] = self::attributes('type', $value['type']);
            $result[$value['id']] = $value;
        }

        return $result;
    }

    /**
     * 处理数据
     * @param $data
     * @return array
     */
    public static function classData($data)
    {
        $sum = 0;
        $className = '';
        $classIdStr = '';
        $langId = 0;
        foreach ($data as $key => $value) {
            $langId = $value['langId'];
            $sum += $value['money'];
            $className .= $value['langName'] . $value['type'] . ',';
            $classIdStr .= $value['id'] . ',';
        }
        return [
            'langId' => $langId,
            'sum' => $sum,
            'className' => trim($className, ','),
            'classIdStr' => trim($classIdStr, ','),
        ];
    }
}